//
//  HeardView.m
//  Travel assistant
//
//  Created by 杨帆 on 16/3/10.
//  Copyright © 2016年 旅游助手. All rights reserved.
//

#import "HeardView.h"

#define kWidth self.frame.size.width
#define kHeight self.frame.size.height


@implementation HeardView


-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor clearColor];
        [self addViews];
        
    }
    
    return self;
}

-(void)addViews{
    
    
    
    //    轮播图
    self.scrollView = [[UIScrollView alloc]init];
    
    self.scrollView.frame = self.bounds;
    
    self.scrollView.contentSize = CGSizeMake(kWidth *4, kHeight);
    
    self.scrollView.backgroundColor = [UIColor clearColor];
    
    
    
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = NO;
    self.scrollView.contentOffset = CGPointMake(0, 0);
    
    self.scrollView.bounces = NO;
    
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.delegate = self;
    
    [self addSubview:self.scrollView];
    for (int i = 0; i < 4; i ++) {
        
        NSString *imageName = [NSString stringWithFormat:@"%d.jpg",i];
        
        UIImageView *imageV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imageName]];
        
        imageV.frame = CGRectMake(kWidth*i, 0, kWidth, kHeight);
        
        [self.scrollView addSubview:imageV];
        
        
    }
    
    
    
    self.pageC = [[UIPageControl alloc]init];
    
    self.pageC.frame = CGRectMake(0,self.frame.size.height - 30 ,self.frame.size.width , 30);
    
    self.pageC.numberOfPages = 4;
    
    self.pageC.backgroundColor = [UIColor clearColor];
    
    self.pageC.alpha = 0.8;
    
    [self.pageC addTarget:self action:@selector(pageAction:) forControlEvents:(UIControlEventValueChanged)];
    
    [self addSubview:self.pageC];
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(changePicture) userInfo:nil repeats:YES];
}


-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    self.pageC.currentPage = scrollView.contentOffset.x/kWidth;
    
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(changePicture) userInfo:nil repeats:YES];
    
}
//定时器方法
-(void)changePicture{
    
    
    int count = (int)self.pageC.currentPage + 1;
    
    
    if (count == 4) {
        count = 0;
    }
    
    self.scrollView.contentOffset = CGPointMake(kWidth*count, 0);
    
    self.pageC.currentPage = count ;
    
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    
    [self.timer invalidate];
    
    self.timer =nil;
    
    
}


-(void)pageAction:(UIPageControl *)sender{
    
    self.scrollView.contentOffset = CGPointMake(kWidth*sender.currentPage, 0);
    
    
}












@end
